package com.xiaoq.sites.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.sites.entity.BannerEntity;
import com.xiaoq.sites.service.IBannerService;
import com.xiaoq.sites.vo.BannerVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/banner")
public class BannerEndpoint {
	
	@Autowired
	private IBannerService bannerSer;
	
	private class BannerListResponse extends GenericListResponse<BannerVo>{}
	
	private static final Logger LOG = LoggerFactory.getLogger(BannerEndpoint.class);
	
	@ApiOperation(value="Banner列表", notes="Banner列表")
	@ApiResponses({
		@ApiResponse(code = 200, response = BannerListResponse.class, message = "Banner列表")
	})
	@GetMapping(value="/{userId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> bannerListView(
			@PathVariable(value="userId",required=false)@ApiParam(value="userId个性化推荐备用",required=false)String userId,
			@RequestParam(value="channelCode",required=true)@ApiParam(value="1000首页Banner2000为发现页Banner",required=true)String channelCode,
			@RequestParam(value="cityCode",required=false)@ApiParam(value="所在市编码（channelCode为20时传）",required=false)String cityCode){
		if(StringUtils.isNotEmpty(channelCode)){
			List<BannerEntity> bannerEntityList = bannerSer.findBannerList(channelCode,cityCode);
			List<BannerVo> bannerVoList = new ArrayList<>();
			BannerVo bannerVo = null;
			for(BannerEntity banner: bannerEntityList){
				bannerVo = new BannerVo();
				BeanUtils.copyProperties(banner, bannerVo);
				bannerVoList.add(bannerVo);
			}
			return BannerListResponse.listNoCount(bannerVoList);
		}else{
			return GenericResponse.ng("请传递具体版面Id");
		}
		
		
	}
	

}
